package search;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;

public class SearchToHtml {


    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage: ConvertToHtml <output_directory>");
            System.exit(-1);
        }

        String outputDir = args[0];
        String outputPath = outputDir + "/output.html";

        try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputPath));
             Scanner scanner = new Scanner(new File(outputDir + "/part-r-00000"))) { // 假设结果在part-r-00000文件中

            writer.write("<!DOCTYPE html>\n<html>\n<head>\n<title>MapReduce Output</title>\n</head>\n<body>\n<table border='1'>\n<tr><th>Key</th><th>Value</th></tr>\n");

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String[] keyValue = line.split("\t", 2); // 假设输出是以tab分隔的键值对
                writer.write("<tr><td>" + escapeHtml(keyValue[0]) + "</td><td>" + escapeHtml(keyValue[1]) + "</td></tr>\n");
            }

            writer.write("</table>\n</body>\n</html>\n");

        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    // 简单的HTML转义函数，实际应用可能需要更全面的转义逻辑
    private static String escapeHtml(String str) {
        return str.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
    }
}
